//
//  Stwx2TableViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/23.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "Stwx2TableViewModel.h"

#import "StwxItemViewModel.h"

@interface Stwx2TableViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation Stwx2TableViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = NO;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = NO;
    
    /// 请求数据
    self.requestDataCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id inputInfo) {
        
        @strongify(self);
        
        NSInteger loadIndex = [inputInfo[@"LoadIndex"] integerValue];
        
        return [self requestListDataSignalWithPageIndex:loadIndex pageSize:2];
    }];
    
    /// 选中
    //    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(ArticleItemViewModel * itemViewModel) {
    //        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    //            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
    //                [subscriber sendNext:itemViewModel];
    //                [subscriber sendCompleted];
    //            });
    //            return nil;
    //        }];
    //    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
}

- (RACSignal *)requestListDataSignalWithPageIndex:(int)pageIndex pageSize:(int)pageSize
{
    @weakify(self)
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self)
        
        NSArray* datas = @[@(pageIndex*3), @(pageIndex*3 + 1), @(pageIndex*3 + 2)];
        
        /// 转化数据
        @weakify(self);
        NSArray *viewModels = [datas.rac_sequence map:^StwxItemViewModel *(NSNumber* number) {
            @strongify(self);
            StwxItemViewModel *itemViewModel = [[StwxItemViewModel alloc] initWithData:[NSString stringWithFormat:@"生态文献-%d", [number intValue]] cellHeight:self.cellHeight];
            
            return itemViewModel;
        }].array;
        
        if (self.shouldPullUpToLoadMore)
        {
            if (0 == pageIndex)
            {
                self.dataSource = viewModels;
            }
            else
            {
                if (self.dataSource == nil)
                {
                    self.dataSource = viewModels;
                }
                else
                {
                    self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
                }
            }
        }
        else
        {
            self.dataSource = viewModels;
        }
        
        [subscriber sendNext:nil];
        [subscriber sendCompleted];
        
        return nil;
    }];
}

@end
